﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using ItemComponent;

namespace Item
{
    public struct STItemData 
    {
        /// <summary>
        /// 道具的标签
        /// </summary>
        public readonly string strItemTag;
        /// <summary>
        /// 道具脚本类型的名称
        /// </summary>
        public readonly string strTypeName;
        /// <summary>
        /// 道具名
        /// </summary>
        public readonly string strItemName;
        /// <summary>
        /// 道具预制路径
        /// </summary>
        public readonly string strPrefabPath;
        /// <summary>
        /// 随机权重
        /// </summary>
        public readonly int nRandomWeight;
        /// <summary>
        /// 道具标签
        /// </summary>
        public readonly string[] arrItemRandomTag;

        public readonly STMyComponetData[] arrMyComponetDatas;

        public readonly JsonData json;

        public STItemData(string tag, string typeName, string itemName, string path,int randomWeight,string[] randomTag,
            STMyComponetData[] myComponetDatas=null)
        {
            strItemTag = tag;
            strItemName = itemName;
            strTypeName = typeName;
            strPrefabPath = path;

            nRandomWeight = randomWeight;
            arrItemRandomTag = randomTag;

            arrMyComponetDatas = myComponetDatas;

            json = new JsonData();

            json["ItemTag_str"] = strItemTag;
            json["TypeName_str"] = strTypeName;
            json["ItemName_str"] = strItemName;
            json["PrefabPath_str"] = strPrefabPath;

            json["RandomWeight_Int"] = nRandomWeight;
            json["ItemRandomTag_arr_Str"] = JsonMapper.ToObject(JsonMapper.ToJson(arrItemRandomTag));

            if (arrMyComponetDatas != null)
            {
                json["MyComponetDatas_Arr"] = new JsonData();
                json["MyComponetDatas_Arr"].SetJsonType(JsonType.Array);

                for (int i = 0; i < arrMyComponetDatas.Length; i++)
                {
                    JsonData tmp = new JsonData();
                    tmp["Type"] = arrMyComponetDatas[i].strType;
                    tmp["Data"] = arrMyComponetDatas[i].data;

                    //Debug.Log(arrMyComponetDatas[i].data.ToJson());
                    //arrTmp[i] = tmp;

                    json["MyComponetDatas_Arr"].Add(tmp);
                }
            }
        }

        public STItemData(JsonData jsonData)
        {
            if (jsonData == null) throw new System.Exception("参数异常");

            json= jsonData;

            strItemTag = jsonData["ItemTag_str"].ToString();
            strTypeName = jsonData["TypeName_str"].ToString();
            strItemName = jsonData["ItemName_str"].ToString();
            strPrefabPath = jsonData["PrefabPath_str"].ToString();

            nRandomWeight = ((int)jsonData["RandomWeight_Int"]);
            arrItemRandomTag = JsonMapper.ToObject<string[]>(jsonData["ItemRandomTag_arr_Str"].ToJson());

            JsonData tmp = json["MyComponetDatas_Arr"];

            arrMyComponetDatas = new STMyComponetData[tmp.Count];
            for (int i = 0; i < tmp.Count; i++)
            {
                arrMyComponetDatas[i] = new STMyComponetData()
                {
                    strType = tmp[i]["Type"].ToString(),
                    data = tmp[i]["Data"],
                };
            }
        }
    }
}